<template>
  <!--月份选择-->
  <div class="month-select-box">
    <div class="month-select-title">
      <p v-text="title"></p>
    </div>
    <div class="month-select" ref="monthSelect">
      <input
        @click="readOnly ? null: selectClick()"
        v-model="value"
        type="text"
        class="select"
        ref="select"
        :placeholder="placeholder_def"
        :disabled="readOnly" readonly>
      <div class="option" ref="myOption">
        <div class="change">
          <div class="change-last" @click="monthsLast()">&lt;</div>
          <div class="month-range" v-text="MonthRange"></div>
          <div class="change-next" @click="monthsNext()">&gt;</div>
        </div>
        <div class="select-option" ref="selOption">
          <span v-for="(item,index) in months" :key="index"
                @click="optionLiClick($event,item)"
                class="months"
                v-text="item">
          </span>
        </div>
      </div>
      <div v-if="!readOnly" class="select-control" ref="control">
        <span class="control01">
            <svg-icon icon-class="arrow-down"/>
            <!-- ☑ -->
          </span>
          <span class="control02" @click="control02Click()">
            <svg-icon icon-class="content-clear"/>
            <!-- × -->
          </span>
      </div>
    </div>
  </div>
</template>

<script>
    export default {
        props: {
            value: {
                type: Object,
                default: () => null
            },
            title: {
                type: String,
                default: () => {
                    return "月份选择"
                }
            },
            placeholder: {
                type: String
            },
            readOnly:{
                type: Boolean,
                default: () => false
            },
            format:{
                type: String,
                default: () => "yyyy-MM"
            }
        },
        data() {
            return {
                dropOpen: true,
                placeholder_def: this.placeholder == null? "请选择" + this.title : this.placeholder,
                currentMonth: null,
                currentYear: new Date().getFullYear(),
                MonthRange : null,
                months: [],
            }
        },
        mounted() {
            this.initMonths(this.currentYear)
            addEventListener('mousedown', (e) => {
              // 监听组件以外的点击事件
              const path=e.path || (e.composedPath && e.composedPath());
              if (!path.includes(this.$refs.myOption) && !path.includes(this.$refs.select)) {
                  if(!this.dropOpen){
                      this.optSelect(false)
                      this.dropOpen = !this.dropOpen
                  }
              }
            })
        },
        methods: {
            initMonths(currentYear){
                let yearMonth = this.format.replace("yyyy",currentYear)
                for(let i = 1; i <= 12; i++) {
                    if (i < 10) {
                        this.$set(this.months,i-1,yearMonth.replace("MM","0" + i))
                    } else {
                        this.$set(this.months,i-1,yearMonth.replace("MM",i))
                    }
                }
                this.MonthRange = currentYear + " 年"
            },
            monthsLast() {
                if (this.currentYear >= 1970) {
                    this.currentYear = this.currentYear - 1
                    this.initMonths(this.currentYear)
                }
            },
            monthsNext() {
                this.currentYear = this.currentYear + 1
                this.initMonths(this.currentYear)
            },
            optSelect() {
                if(this.dropOpen) {
                    this.$refs.control.style.transform="rotate(-180deg)"
                    this.$refs.myOption.style.maxHeight= this.$refs.selOption.offsetWidth + "px"
                    this.$refs.myOption.style.transform="scaleY(1) scale(1)"
                    this.$refs.monthSelect.style.border="1px solid #39404d"
                } else {
                    this.$refs.control.style.transform="rotate(0deg)"
                    this.$refs.myOption.style.maxHeight="0px"
                    this.$refs.myOption.style.transform="scaleY(0) scale(0.5)"
                    this.$refs.monthSelect.style.border="1px solid #2b313e"
                }
            },
            selectClick() {
                if(this.dropOpen) {
                    this.optSelect(true)
                } else {
                    this.optSelect(false)
                }
                this.dropOpen = !this.dropOpen
            },
            control02Click() {
                if(!this.dropOpen){
                    this.optSelect(false)
                    this.dropOpen = !this.dropOpen
                }
                this.label= null
                this.$emit("input", null);
            },
            optionLiClick(e,item) {
                let months = document.getElementsByClassName("months")
                for(let i = 0; i< months.length; i++) {
                    months[i].style.backgroundColor = "transparent";
                }
                e.currentTarget.style.backgroundColor = "#252b38"
                this.value=  item
                this.$emit("input", item);
                // 选择后弹出框消失
                this.optSelect(false)
                this.dropOpen = !this.dropOpen
            },
        }
    }
</script>

<style scoped>
  input, input:focus {
    background: none;
    outline: none;
    border: none;
  }
  ul,li {
    margin: 0;
    padding: 0;
    list-style: none;
  }
  .month-select-box {
    width: 100%;
    height: 30px;
    margin: 15px auto;
  }
  .month-select-title {
    cursor: default;
    float: left;
    width: 20%;
    height: 100%;
    line-height: 30px;
    font-size: 14px;
    letter-spacing: 1.5px;
  }
  .month-select-title p {
    width: 100%;
    margin: 0 0;
    text-align: center;
    color: rgba(255, 255, 255,0.5);
  }
  .month-select {
    cursor: pointer;
    float: left;
    position: relative;
    width: 73%;
    height: 25px;
    /*margin-top: 1.5px;*/
    border: 1px solid #2b313e;
    border-radius: 3px;
    background-color: #222833; /*背景颜色*/
    transition: 0.3s;
  }
  .select {
    cursor: default;
    width: 80%;
    height: 23px;
    background-color: transparent;
    padding-left: 8px;
    padding-right: 8px;
    letter-spacing: 1.5px;
    color: rgba(255, 255, 255,0.5);
    /* 文字溢出处理 */
    display: -webkit-box;
    overflow:hidden;
    text-overflow: ellipsis;
    -webkit-box-orient:vertical;
    -webkit-line-clamp:1;
  }
  .select::placeholder {
    color: rgba(255, 255, 255,0.2);
  }

  .month-text input {
    border: 1px solid #2b313e;
    border-radius: 3px;
    width: 100%;
    height: 20px;
    /*padding-left: 8px;*/
    font-size: 13px;
    letter-spacing: 1.5px;
    color: rgba(255, 255, 255,0.3);
    background-color: transparent;
    transition: 0.3s;
  }
  .month-text input:focus {
    border: 1px solid #39404d;
  }
  .month-text input::placeholder {
    color: rgba(255, 255, 255,0.2);
    font-weight: lighter;
  }

  .option {
    margin-top: 5px;
    width: 100%;
    min-height: 0;
    border: 1px solid #2b313e;
    background-color: #222833;
    box-shadow: 0 5px 10px rgba(0, 0, 0, 0.3);
    border-radius: 3px;
    position: absolute;
    top: 25px;
    transform: scaleY(0) scale(0.5);
    transition: 0.25s;
    z-index: 999;
  }
  .select-option {
    width: calc(100% - 10px);
    /*max-height: 150px;*/
    padding: 5px;
    font-size: 12px;
  }

  .change{
    user-select: none;
    position: relative;
    width: 100%;
    height: 30px;
    font-weight: bolder;
  }
  .change-last,.change-next {
    width: 25px;
    height: 25px;
    font-size: 18px;
    color: rgba(255, 255, 255, 0.2);
    border-radius: 3px;
    position: absolute;
    bottom: 0px;
  }
  .change-last{
    left: 5%;
    text-align: center;
  }
  .change-next{
    right: 5%;
    text-align: center;
  }
  .change-last:hover,.change-next:hover{
    background-color: #1c2430;
    color: rgba(255, 255, 255, 0.5);
  }
  .change-last:active,.change-next:active{
    background-color: #1e2734;
    color: rgba(255, 255, 255, 0.5);
  }
  .months {
    user-select: none;
    letter-spacing: 1px;
    display: inline-block;
    width: 25%;
    height: 40px;
    text-align: center;
    line-height: 40px;
    font-size: 13px;
    color: rgba(255, 255, 255, 0.41);
    border-radius: 3px;
    transition: 0.1s;
  }
  .months:hover{
    background-color: #252b38 !important;
  }
  .month-range{
    /*background-color: rgba(255, 0, 0, 0.51);*/
    width: 60%;
    height: 20px;
    line-height: 20px;
    text-align: center;
    color: rgba(255, 255, 255, 0.3);
    letter-spacing: 1px;
    font-size: 12px;
    position: fixed;
    top:10px;
    left: 0;
    right: 0;
    margin: auto;
  }
  .control02 {
    display: none;
  }
  .select-control {
    user-select: none;
    cursor: pointer;
    width: 18px;
    font-size: 12px;
    height: 18px;
    line-height: 18px;
    text-align: center;
    color: rgba(255, 255, 255,0.2);
    position: absolute;
    top: 3px;
    right: 10px;
    transition: 0.3s;
  }
  .select-control:hover .control01 {
    display: none;
    color: rgba(255, 255, 255,0.5);
  }
  .select-control:hover .control02 {
    display: block;
    color: rgba(255, 255, 255,0.5);
  }

  .select-control:active .control02 {
    color: rgba(255, 255, 255,0.6);
  }
</style>
